<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Init Model
 *
 */


class Listing_model extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	//-- GET PROFILE --//
	public function getProfile($profileID=0){
		$sql = "select * from profile where id = $profileID limit 1";

		if($this->db->query($sql)->row()){
			return $this->db->query($sql)->row();
		}else{
			return false;
		}

	}

	function listing_count($status=0,$id=0){
		$sql = "Select count(*) as total from profile where id=$id and status=$status";
		$query = $this->db->query($sql);
		$row = $query->row();
		if ($row->total != 0)
			return $row->total;
		else
			return '0';
	}

	function update_visit($profileID=0){
		$sql = "update profile set hits = hits + 1 where id=$profileID";
		$this->db->query($sql);
	}

	//-- GET TERM --//
	public function getTerm($profileID=0, $term='ALL', $exclude = 'NONE' ){
		$profileID = (int) $profileID;
		$term = trim($term);

		if($term == 'ALL'){
			if($exclude == 'NONE'){
				$sql = "select * from term where listing_id = $profileID ";
			}else{
				$sql = "select * from term where listing_id = $profileID and term != '$exclude'";
			}
		}else{
			$sql = "select * from term where listing_id = $profileID and term = '$term' ";
		}

		$result = $this->db->query($sql)->result();

		if(count($result)  == 1){
			return $this->db->query($sql)->row();
		}
		elseif(count($result)  > 1){
			return $result;
		}else{
			return FALSE;
		}
	}

	//-- GET LISTING --TAG ---//
	public function getTag($profileID=0, $limit=99){
		$sql = "select * from term where listing_id = $profileID and term = 'key' limit 2, $limit";
		$result = $this->db->query($sql)->result();

		if($result){
			return $result;
		}else{
			return null;
		}
	}




	public function randomTag($profileID=0){
		$sql = "select term_value from term where listing_id = $profileID and term = 'key' limit 2, 1";
		$query = $this->db->query($sql);
		if($query){
			return $query->row();
			//die();
		}
	}

	//-- Get Listing (CATEGORY | SEARCH )
	public function getListing($catID=0,$limit=10,$offset=0){

		$cityID = 0;

		if($this->input->cookie('cook_location')){
			$cityID = $this->input->cookie('cook_location');
		}

		if($catID == 0){
			return FALSE;
		}else{

			if($cityID !=0){
				$sql = "SELECT t1.* FROM profile AS t1
				INNER JOIN term AS t2 ON t1.id = t2.listing_id
				WHERE t2.term =  'key'
				AND t2.term_value =  $catID  and t1.city = $cityID	 LIMIT $offset, $limit" ;

			}else{
				$sql = "SELECT t1.* FROM profile AS t1
				INNER JOIN term AS t2 ON t1.id = t2.listing_id
				WHERE t2.term =  'key'
				AND t2.term_value =  $catID LIMIT $offset, $limit" ;
			}

		}




		$result = $this->db->query($sql)->result();

		if($result){
			return $result;
		}else{
			return FALSE;
		}

	}

	public function updateHits($profileID=0){
		$sql  =  "update profile set hits = hits +1 where id = $profileID";
		$this->db->query($sql);
	}


	function getListing_deatils($profile_id=0)
	{
		$sql = "select * from profile where id= $profile_id and status=51 limit 1";
		$result = $this->db->query($sql)->row();
		if($result){
			return $result;
		}else{
			return false;
		}
	}

	//--Get Listing Keyword Exclude Category and SubCategiry--//
	function getkeyword($profile_id=0,$cat,$sub_cat){
		$sql="select term_value from term where listing_id=$profile_id and term_value not in($cat,$sub_cat)";
		$result=$this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}
	function getKeywordAll($profile_id=0,$limit=5){
		$sql="select term_value from term where listing_id=$profile_id and term='key' and caption='Keyword' limit $limit";
		$result=$this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}
	}


	function getCategory($id){
		$sql="select id,name from category_tree where id=$id limit 1";
		if($this->db->query($sql)->row()){
			return $this->db->query($sql)->row();
		}else{
			return "Error!";
		}
	}

	function getQualification($profile_id=0){

		$sql = "select * from qualification where profile_id = $profile_id";
		$result=$this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}

	}

	function getAwards($profile_id=0){

		$sql = "select * from awards where profile_id = $profile_id";
		$result=$this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}

	}
	function getLikes($profile_id=0,$mem_id=0){
		if($this->session->userdata('mem_id')){
		$mem_id = $this->session->userdata('mem_id');
		$sql = "select * from like_counter where liked_on = $profile_id and liked_by = $mem_id";
		}
		$sql = "select * from like_counter where liked_on = $profile_id and liked_by = $mem_id";
		$result=$this->db->query($sql)->row();
		if($result){
			return $result;
		}else{
			return false;
		}

	}


	function likedata($id=NULL){

		$mem_id = $this->session->userdata('mem_id');

		$sql = "delete from like_counter where liked_on=$id and liked_by = $mem_id";
		$this->db->query($sql);
	}

	function like_count_memwise($likes='Like',$profile_id=0){
		
		//if($this->session->userdata('mem_id')){
		//$mem_id = $this->session->userdata('mem_id');
		//$sql = "Select count(*) as total from like_counter where liked_on = $profile_id and liked_by = $mem_id ";
	//	}
		$sql = "Select count(*) as total from like_counter where liked_on = $profile_id and likes = $likes";
		
		
		$query = $this->db->query($sql);
		$row = $query->row();
		if ($row->total != 0)
			return $row->total;
		else
			return '0';
	}

	function getAssociations($profile_id=0){

		$sql = "select * from association where profile_id = $profile_id";
		$result=$this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}

	}

	function getLicense($profile_id=0){

		$sql = "select * from license where profile_id = $profile_id";
		$result=$this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}

	}

	function getReviews($profile_id=0){

		$sql = "select * from review_master where profile_id = $profile_id and status = 51";
		$result=$this->db->query($sql)->result();
		if($result){
			return $result;
		}else{
			return false;
		}

	}

	function get_mail($email) {
		if (empty($email)) {
			return $email;
		} else {
			$sql = "Select * from profile where email = '$email'";
			$query = $this->db->query($sql);
			if ($query->num_rows() > 0) {
				$row = $query->row();
				return $row->email;
			}
		}
	}


	function get_email($fieldName) {
		$sql = "SELECT $fieldName FROM profile";
		$query = $this->db->query($sql);
		$result = $query->result();
		$row = $query->row();
		return $row->$fieldName;
	}

	//-- GET REVIEW RATTING ALL EXPERIENCE WISE--
	function count_reviews($profile_id=Null) {
		$this->db->where('profile_id', $profile_id);
		$query = $this->db->get('review_master');
		if ($query->num_rows() > 0) {
			return $query->num_rows();
		} else {
			return 0;
		}
	}
	function count_all_listing($key_id = 0, $type=NULL) {

		$sql = "Select count(*)as total from term where term_value = $key_id";
		$query = $this->db->query($sql);
		$row = $query->row();
		if ($row->total != 0)
			return $row->total;
		else
			return '0';
	}

	function getCity($id){
		$sql="select cityname from city where id=$id limit 1";
		if($this->db->query($sql)->row()){
			return $this->db->query($sql)->row()->cityname;
		}else{
			return "Error!";
		}
	}
	function getRatting($listing_id=0){
		$sql = "SELECT experience, COUNT( experience ) AS no
		FROM  `review_master`
		WHERE profile_id =$listing_id AND STATUS =51
		GROUP BY experience
		HAVING COUNT( * ) > 0 ORDER BY experience DESC ";

		$result=$this->db->query($sql)->row();
		if($result){
			return $result;
		}else{
			return false;
		}
	}

	function membership_facility($listing_id, $fieldName) {

		$this->db->select('membership');
		$this->db->where('id', $listing_id);
		$query = $this->db->get('profile');
		$mem_id = $query->row()->membership;

		$sql = "SELECT $fieldName FROM membership where id=$mem_id";
		$query = $this->db->query($sql);
		$result = $query->result();
		$row = $query->row();
		return $row->$fieldName;
	}

}
/* End of file*/
